package net.seehope.foodie.controller.async;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;


/*
 * 伪造的队列
 * 
 */



//構建一個模擬隊列，使用Component将其放入容器，默认是单例的
@Component
public class MockQueue {
	
	private static final Logger log = LoggerFactory.getLogger(MockQueue.class);


	/*
	 * 存放订单号的区域，应用2会一直监听(轮询),
	 * 里面有值代表有订单需要处理
	 */
	private String placeOrder;
	
	
	/*
	 * 返回结果的区域，应用1会一直监听，
	 * 里面有值代表有订单已经完成，需要将结果返回给前端
	 */
	
	private String completeOrder;


	public String getPlaceOrder() {
		return placeOrder;
	}


	public void setPlaceOrder(String placeOrder) {
		//模拟应用2处理订单的逻辑，并启动线程
		
		//创建一个新线程
		new Thread(()->{
			log.info("接收到了下单请求中的订单号，应用2开始处理订单");
			
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
			this.completeOrder = placeOrder;
			
			log.info("应用2处理完订单，订单号为"+ placeOrder);
			
		}).start();
		
		
	}


	public String getCompleteOrder() {
		return completeOrder;
	}


	public void setCompleteOrder(String completeOrder) {
		this.completeOrder = completeOrder;
	}
	
	
	
	
}
